********************************************************************************
*                                                                              *
*              FIRM-LEVEL ANALYSES OF THE IMPACT OF ROBOT ADOPTION             *
*                       OSTER's BIAD ADJUSTED REGRESSIONS                      *
*                                                                              *
********************************************************************************

* Create Table B8

cd "H:\"
clear all
set maxvar 100000

// Variable list for fixed effects regressions
global depvar logvalueadded laborshare loghwage loghoursworked
global feweightvar hoursworked09
global fecvarcompete beid sbi2#year mun#year c.logvalueaddedpworker09#year c.logworkers09#year
global cluster beid#year
global controls logvalueaddedpworker09_* logworkers09_* logcinv_computers logcinv_mach logcinv_transport logcinv_realestate logfirmage
global runs = 100

set seed 1 
use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1
drop if logvalueaddedpworker09==. | logworkers09==.

tab year, g(year_)
drop year_1

capture g logcinv_realestate = ln(cinv_realestate)
capture g logcinv_transport = ln(cinv_transport)

g run = .
foreach var in $depvar {
	g β_`var' = .
	g γ_`var' = .
}

forvalues year = 2009(1)2020 {
	g logvalueaddedpworker09_`year' = logvalueaddedpworker09*(year==`year')
	g logworkers09_`year' = logworkers09*(year==`year')
}

replace $feweightvar = int($feweightvar)

global ds_controls
global dc_controls
foreach var in $controls {
	global ds_controls $ds_controls ds_`var'
	global dc_controls $dc_controls dc_`var'
}

foreach var in logcinv_computers logcinv_mach logcinv_transport logcinv_realestate logfirmage {
	drop if `var'==.
}


qui forvalues r = 0(1)$runs {
nois _dots `r' 0

preserve
	if `r' > 0 {
		bsample
	}
	
	hdfe $depvar robots $controls [weight=hoursworked09], absorb(beid year) g(ds_)
	hdfe $depvar robots $controls [weight=hoursworked09], absorb(beid sbi4#year mun#year) g(dc_)
	
	foreach var in $depvar {
		// Baseline
		reghdfe `var' robots $controls [weight=$feweightvar], absorb(beid year)
		local r2max = e(r2)
		
		regress ds_`var' ds_robots $ds_controls [weight=$feweightvar]
		local r2max = e(r2)+(1-`r2max')
				
		psacalc beta ds_robots, rmax(`r2max') delta(1)
		local β_`var' = r(beta)	
		
		// More detailed fixed effects
		reghdfe `var' robots $controls [weight=$feweightvar], absorb(beid sbi4#year mun#year)
		local r2max = e(r2)
		
		regress dc_`var' dc_robots $dc_controls [weight=$feweightvar]
		local r2max = e(r2)+(1-`r2max')
				
		psacalc beta dc_robots, rmax(`r2max') delta(1)
		local γ_`var' = r(beta)
	}
restore

if `r' == 0	{
	local n = `r'+1	
}
else {
	local n = `r'+5
}

replace run = `r' in `n'
foreach var in $depvar {
	replace β_`var' = `β_`var'' in `n'
	replace γ_`var' = `γ_`var'' in `n'
}

}

drop if _n > 5+$runs
keep run β_* γ_*
qui foreach var in $depvar {
	su β_`var' if _n > 5
	replace β_`var' = r(sd) in 2
	su γ_`var' if _n > 5
	replace γ_`var' = r(sd) in 2
}
tostring run, force replace
replace run = "β" in 1
replace run = "σ" in 2
replace run = "" if run == "."

export excel using "Robots and Workers\Results\Tables.xlsx", sheet("Oster_Raw") sheetmodify cell(A5) firstrow(variables)
